package com.hoo.util;

import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;

public class AESUtils {

    private static Provider provider = new BouncyCastleProvider();

    private static final String KEY_ALGORITHM = "AES";
    private static final int KEYSIZE = 128;
    private static final String CIPHER_ALGORITHM = KEY_ALGORITHM + "/ECB/PKCS5Padding";

    private static byte[] key = null;
    static {
        // 加载key
        getKey();

    }
	/**
	 * 解密
	 * @param data
	 * @return
	 * @throws Exception
	 */
    public static String decrypt(String data) throws Exception {
        if (StringUtils.isBlank(data)) {
            return "";
        }
        Key k = toKey(key);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, provider);
        cipher.init(Cipher.DECRYPT_MODE, k);

        return new String(cipher.doFinal(Base64.decode(data.getBytes())));
    }

    /**
     * 加密
     * @param data
     * @return
     * @throws Exception
     */
    public static String encrypt(String data) throws Exception {
        if (StringUtils.isBlank(data)) {
            return "";
        }
        Key k = toKey(key);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, provider);
        cipher.init(Cipher.ENCRYPT_MODE, k);

        return Base64.encodeBytes(cipher.doFinal(data.getBytes()));
    }

    private static Key toKey(byte[] key) throws Exception {
        SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
        return secretKey;
    }

    private static void getKey() {

        String userDir = System.getProperty("user.home");

        File keysDir = new File(userDir + "/.keys/s.key");

        if (keysDir.exists()) {

            FileInputStream fis = null;
            try {
                fis = new FileInputStream(keysDir);
                key = new byte[fis.available()];
                fis.read(key);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (Exception e2) {
                    } finally {
                        fis = null;
                    }
                }
            }

        } else {
            KeyGenerator kg;
            FileOutputStream fos = null;
            try {
                kg = KeyGenerator.getInstance(KEY_ALGORITHM, provider);
                kg.init(KEYSIZE);
                Key k = kg.generateKey();

                key = k.getEncoded();

                fos = new FileOutputStream(keysDir);
                fos.write(key);

            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        fos = null;
                    }
                }
            }
        }

    }
}
